﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;

namespace PVP
{
    class myMath
    {
        /// <summary>
        /// 算术平均值(二维Point)
        /// </summary>
        /// <param name="darr"></param>
        /// <returns></returns>
        public static Point2D ave(IList<Point2D> ptList)
        {
            double sumX = 0.0f;
            double sumY = 0.0f;
            int couter = 0;
            foreach (Point2D pt in ptList)
            {
                sumX += pt.X;
                sumY += pt.Y;
                couter++;
            }
            return new Point2D(sumX / couter, sumY / couter);
        }


        public static Point2D std(IList<Point2D> ptList, double tolerance)
        {
            Point2D avePt = myMath.ave(ptList);
            List<Point2D> ptList2 = new List<Point2D>();
            foreach (Point2D pt in ptList)
            {
                if (Math.Sqrt((pt.X - avePt.X) * (pt.X - avePt.X) + (pt.Y - avePt.Y) * (pt.Y - avePt.Y)) <= tolerance)
                {
                    ptList2.Add(pt);
                }
            }
            return myMath.ave(ptList2);
        }

        /// <summary>
        /// 算术平均值(一维)
        /// </summary>
        /// <param name="darr"></param>
        /// <returns></returns>
        public static float ave(float[] darr)
        {
            float sum = 0.0f;
            float ave = 0.0f;
            for (int i = 0; i < darr.Length; i++)
            {
                sum += darr[i];
            }
            ave = sum / darr.Length;
            return ave;
        }

        /// <summary>
        /// 标准差(一维)
        /// </summary>
        /// <param name="coor"></param>
        /// <returns></returns>
        public static float std(float[] darr)
        {
            float sum = 0.0f;
            float ave = myMath.ave(darr);
            for (int i = 0; i < darr.Length; i++)
            {
                sum += (darr[i] - ave) * (darr[i] - ave);
            }
            return (float)Math.Sqrt(sum / darr.Length);
        }

        /// <summary>
        /// 排除超差的值后,重新计算的平均值(一维)
        /// </summary>
        /// <param name="darr"></param>
        /// <param name="std"></param>
        /// <param name="min"></param>
        /// <returns></returns>
        public static float limit(float[] darr, float min)
        {
            int counter = 0;
            float sum = 0.0f;
            float std = myMath.std(darr);
            for (int i = 0; i < darr.Length; i++)
            {
                if (Math.Abs(darr[i] - std) < min)
                {
                    sum += darr[i];
                    counter++;
                }
            }
            return sum / counter;
        }

       
    }
}
